//
//  MyStatusCell.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/6.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit
import SnapKit

class MyStatusCell: UITableViewCell {

    //属性
    //底部按钮顶部约束
    private var bottomTopView: Constraint?
    
    var status: MyStatus?{
        didSet{
            //当控制器设置cell时，将模型传给originalView
            original.status = status
            
            //判断是否显示被转发内容
            bottomTopView?.uninstall()
            if let _ = status?.retweeted_status{
                //当控制器设置cell时，将模型传给retweetView
                retweetView.status = status?.retweeted_status
                
                retweetView.hidden = false
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomTopView = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else {
            retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomTopView = make.top.equalTo(original.snp_bottom).constraint
                })
            }
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }
    
    func prepareUI() {
        //将original加到contentView
        contentView.addSubview(original)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        //原创微博
        original.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(contentView)
            
            make.top.equalTo(contentView).offset(10)
//            make.height.equalTo(44)
        }
        //转发微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(original.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        //底部按钮约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            self.bottomTopView = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        //自身约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            //底部要和original重合
            make.bottom.equalTo(bottomView)
        }
        
    
    }
    //MARK: - 懒加载
    //原创微博
    private lazy var original: MyStatusOriginalView = MyStatusOriginalView()
    //转发
    private lazy var retweetView: MyStatusRetweetView = MyStatusRetweetView()
    //底部按钮
    private lazy var bottomView: MyStatusBottomView = MyStatusBottomView()

}
